package com.dim.list_orders;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class backup_db extends Activity {

	private class_function funct = new class_function();
	private ListView listView;
	private List<String> files_arr = new ArrayList<String>();
	private List<String> name_arr = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(com.dim.list_orders.R.layout.list_save);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				com.dim.list_orders.R.layout.title);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(com.dim.list_orders.R.string.l_main);
		listView = (ListView) findViewById(R.id.list);
		browseTo(new File(Environment.getExternalStorageDirectory()
				+ "/Backup/" + this.getPackageName() + "/"));

		registerForContextMenu(listView);
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.list) {
			menu.setHeaderTitle(com.dim.list_orders.R.string.menu_management);
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(com.dim.list_orders.R.menu.menu_delete, menu);
		}
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {

		case com.dim.list_orders.R.id.b_delete:
			delete(info.id);
			break;
		}
		return true;
	}

	private void delete(final long id) {
		new AlertDialog.Builder(this)
				.setTitle(
						getString(com.dim.list_orders.R.string.d_delete_title))
				.setMessage(
						getString(com.dim.list_orders.R.string.d_delete_text))
				.setNegativeButton(android.R.string.no, null)
				.setPositiveButton(android.R.string.yes, new OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						funct.file_delete(getBaseContext(),
								files_arr.get((int) id));
						browseTo(new File(Environment
								.getExternalStorageDirectory()
								+ "/Backup/"
								+ getBaseContext().getPackageName() + "/"));
					}
				}).create().show();
	}

	private void browseTo(final File aDirectory) {
		if (aDirectory.isDirectory()) {
			File[] files = aDirectory.listFiles();
			Arrays.sort(files, new Comparator<File>() {
				public int compare(File f1, File f2) {
					return Long.valueOf(f1.lastModified()).compareTo(
							f2.lastModified());
				}
			});
			fill(files);
		}
	}

	private void fill(File[] files) {
		files_arr.clear();
		name_arr.clear();
		for (File file : files) {
			files_arr.add(file.getAbsolutePath());
			name_arr.add(file.getName().substring(0, 10));
		}

		ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, name_arr);

		listView.setAdapter(directoryList);

		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

	}

	public void b_add(View v) {

		for (int i = 0; i < files_arr.size(); i++) {
			if (listView.isItemChecked(i)) {
				funct.file_restory(this, files_arr.get(i),
						getString(com.dim.list_orders.R.string.db_name));
			}
		}
		onBackPressed();

		// funct.file_delete_old(new File(Environment
		// .getExternalStorageDirectory()
		// + "/Backup/"
		// + getBaseContext().getPackageName() + "/"));

	}

}
